﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.IDAL;
using Register.Model;
using Register.DBUtility;
using Register.Command;
using System.Data;
using System.Data.SqlClient;
namespace Register.DAL
{
    /// <summary> 
    /// FileName: DoctorPersonInfoDAL.cs    
    /// CLRVersion: 4.0.30319.269    
    /// Author: DoubleZ    
    /// Corporation:     
    /// Description:     
    /// DateTime: 2012/7/7 10:02:08    
    /// </summary>
  public  class DoctorPersonInfoDAL:IDoctorPersonInfoDAL
    
  {
      
      #region 获取医生用户信息

      public  List<DoctorUserInfo> GetDoctorUserInfo(string doctorUserID) {
          String sql = "   select  top 1 DoctorUser_ID,Doctor_ID,DoctorUser_UserName,DoctorUser_Password,DoctorUser_LastRegTime from System_doctorUserInfo where DoctorUser_ID='" + doctorUserID + "'";
            List<DoctorUserInfo> list = new List<DoctorUserInfo>();
            SqlDataReader reader = DbHelperSQL.ExecuteReader(sql);
            list = ChangeReaderToDoctorUserList(reader);
            reader.Close();
            return list;

      }
       
       private List<DoctorUserInfo> ChangeReaderToDoctorUserList(SqlDataReader reader)
        {
            List<DoctorUserInfo> list = new List<DoctorUserInfo>();
            while (reader.Read())
            {
               DoctorUserInfo paramter = new DoctorUserInfo();
               paramter.DoctorUser_ID=reader.GetString(0);
                paramter.Doctor_ID=reader.GetString(1);
                paramter.DoctorUser_UserName=reader.GetString(2);
                paramter.DoctorUser_Password = reader.GetString(3);
                //临时赋值
                paramter.DoctorUser_LastRegTime=DateTime.Now;//Convert.ToDateTime(reader.GetString(4));
                list.Add(paramter);
            }
            return list;
        }
      #endregion

      #region 获取医生信息
     public List<DoctorInfo> GetDoctorInfo(string doctorID) {
         String sql = " select  top 1 Departments_ID,Doctor_No,Doctor_Name,Doctor_Gender,Doctor_DateOfBirth,Doctor_Position,Doctor_Education,Doctor_Resume,Doctor_Phone,Doctor_Email,Doctor_Picture,Doctor_Specialty from System_doctorInfo  where Doctor_ID='" + doctorID + "'";
            List<DoctorInfo> list = new List<DoctorInfo>();
            SqlDataReader reader = DbHelperSQL.ExecuteReader(sql);
            list = ChangeReaderToDoctorInfoList(reader);
            reader.Close();
            return list;
      
      }
     private List<DoctorInfo> ChangeReaderToDoctorInfoList(SqlDataReader reader)
        {
            List<DoctorInfo> list = new List<DoctorInfo>();
            while (reader.Read())
            {
                DoctorInfo paramter = new DoctorInfo();
                paramter.Departments_ID = reader.GetString(0);
                paramter.Doctor_No = reader.GetString(1);
                paramter.Doctor_Name = reader.GetString(2);
                paramter.Doctor_Gender = reader.GetString(3);
                paramter.Doctor_DateOfBirth = reader.GetDateTime(4);
                paramter.Doctor_Position = reader.GetString(5);
                paramter.Doctor_Education = reader.GetString(6);
                paramter.Doctor_Resume = reader.GetString(7);
                paramter.Doctor_Phone = reader.GetString(8);
                paramter.Doctor_Email = reader.GetString(9);
                paramter.Doctor_Picture = reader.GetString(10);
                paramter.Doctor_Specialty = reader.GetString(11);
                list.Add(paramter);
            }
            return list;
        }
      #endregion
      #region 更新医生个人信息
      /// <summary>
      /// 更新医生用户信息
      /// </summary>
      /// <param name="doctorUser"></param>
      /// <returns></returns>
    public int UpdateDoctorUserInfo(DoctorUserInfo doctorUserInfo) { 
         StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_doctorUserInfo set ");
            strSql.Append("DoctorUser_UserName=@DoctorUser_UserName,");
            strSql.Append("DoctorUser_Password=@DoctorUser_Password,");
            strSql.Append("DoctorUser_LastAmendPerson=@DoctorUser_LastAmendPerson, ");
            strSql.Append("DoctorUser_LastAmendTime=@DoctorUser_LastAmendTime");
            
            strSql.Append(" where DoctorUser_ID=@DoctorUser_ID");
            SqlParameter[] parameters = {	
					new SqlParameter("@DoctorUser_UserName", SqlDbType.VarChar,20),
					new SqlParameter("@DoctorUser_Password", SqlDbType.Char,32),
					new SqlParameter("@DoctorUser_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@DoctorUser_LastAmendTime", SqlDbType.DateTime),
					
                    new SqlParameter("@DoctorUser_ID", SqlDbType.Char,14)
                                       };

            parameters[0].Value = doctorUserInfo.DoctorUser_UserName;

            parameters[1].Value = doctorUserInfo.DoctorUser_Password;
            parameters[2].Value = doctorUserInfo.DoctorUser_LastAmendPerson;
            parameters[3].Value = doctorUserInfo.DoctorUser_LastAmendTime;
          
            parameters[4].Value = doctorUserInfo.DoctorUser_ID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
  
     /// <summary>
     ///更新医生信息 
     /// </summary>
     /// <param name="doctorUser"></param>
     /// <returns></returns>
   public int UpdateDoctorInfo(DoctorInfo doctorInfo) { 
    
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update System_doctorInfo set ");
            strSql.Append("Doctor_Gender=@Doctor_Gender, ");
            strSql.Append("Doctor_DateOfBirth=@Doctor_DateOfBirth, ");
          
            strSql.Append("Doctor_Specialty=@Doctor_Specialty, ");
            strSql.Append("Doctor_Resume=@Doctor_Resume, ");

            strSql.Append("Doctor_Phone=@Doctor_Phone, ");
            strSql.Append("Doctor_Email=@Doctor_Email, ");
            strSql.Append("Doctor_LastAmendPerson=@Doctor_LastAmendPerson,");
            strSql.Append("Doctor_LastAmendTime=@Doctor_LastAmendTime");
            strSql.Append(" where Doctor_ID=@Doctor_ID");
            SqlParameter[] parameters = {	
				
					new SqlParameter("@Doctor_Gender", SqlDbType.Char,4),
					new SqlParameter("@Doctor_DateOfBirth", SqlDbType.DateTime),
					
					new SqlParameter("@Doctor_Specialty", SqlDbType.NVarChar,100),
					new SqlParameter("@Doctor_Resume", SqlDbType.VarChar,200),
					new SqlParameter("@Doctor_Phone", SqlDbType.Char,11),
					new SqlParameter("@Doctor_Email", SqlDbType.VarChar,30),
			
                    new SqlParameter("@Doctor_LastAmendPerson", SqlDbType.VarChar,20),
					new SqlParameter("@Doctor_LastAmendTime", SqlDbType.DateTime),
                    new SqlParameter("@Doctor_ID", SqlDbType.Char,14),
                                       };


    
            parameters[0].Value = doctorInfo.Doctor_Gender;
            parameters[1].Value = doctorInfo.Doctor_DateOfBirth;
      
          
            parameters[2].Value = doctorInfo.Doctor_Specialty;
            parameters[3].Value = doctorInfo.Doctor_Resume;
            parameters[4].Value = doctorInfo.Doctor_Phone;
            parameters[5].Value = doctorInfo.Doctor_Email;
          
            parameters[6].Value = doctorInfo.Doctor_LastAmendPerson;
            parameters[7].Value = doctorInfo.Doctor_LastAmendTime;
            parameters[8].Value = doctorInfo.Doctor_ID;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
    

      #endregion
    #region 获取患者评价信息
   public List<Model.EvaluationInfo> GetPagedEvaluationInfo(int size, int pageIndex, string where) {
       return DataSetToList(DbHelperSQL.getPagedData(" * ", "EvaluationInfo_AddTime", where, "System_evaluationInfo", OrderBy.DESC, pageIndex, size));
   }

  public int GetEvaluationInfoCount(string where) {
      int count = 0;
      StringBuilder str = new StringBuilder();
      str.Append(" select count(*) from System_evaluationInfo");
      if (where != null)
      {
          str.Append(" where ");
          str.Append(where);
          count = (int)DbHelperSQL.GetSingle(str.ToString());
      }
      else
      {
          count = (int)DbHelperSQL.GetSingle(str.ToString());
      }
      return count;
  }
  private List<Model.EvaluationInfo> DataSetToList(DataSet ds)
  {
      List<Model.EvaluationInfo> list = new List<Model.EvaluationInfo>();
      foreach (DataRow dr in ds.Tables[0].Rows)
      {
          EvaluationInfo evaluation = new EvaluationInfo();
          evaluation.Doctor_ID = (string)dr["Doctor_ID"];
          evaluation.EvaluationInfo_AddTime = (DateTime)dr["EvaluationInfo_AddTime"];
          evaluation.EvaluationInfo_EvalContent = (string)dr["EvaluationInfo_EvalContent"];
          evaluation.EvaluationInfo_EvalWay = (string)dr["EvaluationInfo_EvalWay"];
          evaluation.EvaluationInfo_ID = (string)dr["EvaluationInfo_ID"];
          evaluation.EvaluationInfo_Status = (string)dr["EvaluationInfo_Status"];
          evaluation.PatientUserInfo_ID = (string)dr["PatientUserInfo_ID"];

          list.Add(evaluation);
      }
      return list;
  }


      #endregion
      #region 根据患者ID获取患者姓名
     public string GetPatientNameById(string patientID) { 
   string strSql = "select PatientUserInfo_Name  from System_patientUserInfo where PatientUserInfo_ID='"+patientID+"'";
     DataSet ds = DbHelperSQL.Query(strSql);
     string PatientUserInfo_Name = "无";
     if (ds != null)
     {
         PatientUserInfo_Name= ds.Tables[0].Rows[0]["PatientUserInfo_Name"].ToString();

     }
     return PatientUserInfo_Name;
        }
   
  
      #endregion
      #region 根据评价ID获取评价详情
    public string GetEvaluationInfoById(string evaluateID) { 
        string strSql = "select EvaluationInfo_EvalContent  from System_evaluationInfo where EvaluationInfo_ID='"+evaluateID+"'";
     DataSet ds = DbHelperSQL.Query(strSql);
     string EvaluationInfo_EvalContent = "无";
     if (ds != null)
     {
         EvaluationInfo_EvalContent= ds.Tables[0].Rows[0]["EvaluationInfo_EvalContent"].ToString();

     }
     return EvaluationInfo_EvalContent;
        }
     
      #endregion
  }
}
